package com.gxa.opo.hualian.Dao.Imlp;

import com.gxa.opo.hualian.Dao.UserDao;
import com.gxa.opo.hualian.entity.User;
import com.gxa.opo.hualian.unit.DBUtil;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImlp implements UserDao {
    @Override
    public List<User> SelectAll() throws SQLException, ClassNotFoundException {

        String sql = "SELECT id,name,phone,pwd,regTime,points FROM tb_user";

        DBUtil dbUtil = new DBUtil();

        PreparedStatement preparedStatement = dbUtil.createPreparedStatement(sql);

        ResultSet resultSet = dbUtil.execQuery(preparedStatement);

        List<User> list = new ArrayList<>();

        while (resultSet.next()){
            User user = new User();

            user.setId(resultSet.getInt("id"));
            user.setName(resultSet.getString("name"));
            user.setPhone(resultSet.getString("phone"));
            user.setPwd(resultSet.getString("pwd"));
            user.setRegTime(resultSet.getDate("regTime"));
            user.setPoints(resultSet.getInt("points"));
            list.add(user);
        }
        dbUtil.close();
        return list;
    }

    @Override
    public void deleteById(Integer id) throws SQLException, ClassNotFoundException {
        String sql = "DELETE FROM tb_user WHERE id = ?";

        DBUtil dbUtil = new DBUtil();

        PreparedStatement preparedStatement = dbUtil.createPreparedStatement(sql);

        preparedStatement.setInt(1,3);

        if(dbUtil.execUpdate(preparedStatement) >= 0){
            System.out.println("影响行数：" + dbUtil.execUpdate(preparedStatement));
        }else {
            System.out.println("操作失败");
        }
    }
}
